package com.xbongbong.paas.controller;

import com.alibaba.fastjson.JSON;
import com.xbongbong.admin.pojo.dto.RaidersProductExchangeDTO;
import com.xbongbong.admin.pojo.dto.RaidersProductListDTO;
import com.xbongbong.admin.pojo.vo.RaidersProductExchangeVO;
import com.xbongbong.admin.pojo.vo.RaidersProductListVO;
import com.xbongbong.admin.service.RaidersProductService;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.wrap.XbbResponse;
import com.xbongbong.pro.constant.XbbProConstant;
import com.xbongbong.pro.enums.errorcodes.SystemErrorCodeEnum;
import org.springframework.http.MediaType;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.Objects;

/**
 * @author 徐俊杰
 * @version v1.0
 * @date 2019/9/20 15:37
 * @since v1.0
 */
@RestController
@RequestMapping(XbbProConstant.API_ROOT_PREFIX + "/raiders/product")
public class RaidersProductController {
    @Resource
    private RaidersProductService raidersProductService;

    @RequestMapping(value = "/list", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String list(@RequestBody @Valid RaidersProductListDTO raidersProductListDTO, BindingResult br) {
        XbbResponse<RaidersProductListVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                RaidersProductListVO raidersProductListVO = raidersProductService.list(raidersProductListDTO);
                response = new XbbResponse<>(raidersProductListVO);
            } catch (XbbException e){
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     * 商品兑换
     * @param raidersProductExchangeDTO
     * @param br
     * @return java.lang.String
     * @author zcp
     * @date 19/10/17 017 10:41
     * @update 19/10/17 017 10:41
     * @since v1.0
     * @version v1.0
     */
    @RequestMapping(value = "/exchange", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String exchange(@RequestBody @Valid RaidersProductExchangeDTO raidersProductExchangeDTO, BindingResult br) {
        XbbResponse<RaidersProductExchangeVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                RaidersProductExchangeVO raidersProductExchangeVO = raidersProductService.exchange(raidersProductExchangeDTO);
                response = new XbbResponse<>(raidersProductExchangeVO);
            } catch (XbbException e){
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }
}
